<template>
	<view>
		<uni-search-bar style="background-color: #fff;" placeholder="请输入客户名称进行查询" @confirm="getDataList"
			v-model="queryParam.Keyword" @cancel="cancel">
		</uni-search-bar>
		<uni-list>
			<uni-list-item v-for="item in data" :key="item.Id" :title="item.cCusName" :note="item.cCusPhone" showArrow
				clickable @click="toForm(item)" />
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: [],
				pagination: {
					PageIndex: 1,
					PageRows: 30,
					SortField: "CreateTime",
					SortType: "desc",
				},
				total: 0,
				queryParam: {
					"Condition": "cCusName",
					"Keyword": "",
					"SupplieId": "",
				},
				// 从订单详情传递过来的
				selectType: "",
				openOrder: ""
			}
		},
		onNavigationBarButtonTap(e) {
			console.log(e);
			uni.navigateTo({
				url: '/pages/Information/page/CustomerForm'
			})
		},
		onLoad(options) {
			console.log('获取options', options);
			this.openOrder = options.openOrder
			this.selectType = options.type
			this.queryParam.SupplieId = uni.getStorageSync('CompanyInfo').Id
		},
		onShow() {
			this.pagination.PageIndex = 1
			this.getDataList()
		},
		onReachBottom() {
			if (this.data.length >= this.total) return
			this.pagination.PageIndex++
			uni.showLoading()
			this.$http('post', '/BO/CustomerInfo/GetAppCusinfoData', {
					...this.pagination,
					Search: this.queryParam
				})
				.then(res => {
					uni.hideLoading()
					if (res.Success) {
						this.data = this.data.concat(res.Data)
						this.total = res.Total
					} else uni.showToast({
						icon: 'none',
						title: res.Msg
					})
				})
		},
		methods: {
			getDataList() {
				uni.showLoading()
				this.$http('post', '/BO/CustomerInfo/GetAppCusinfoData', {
						...this.pagination,
						Search: this.queryParam
					})
					.then(res => {
						uni.hideLoading()
						if (res.Success) {
							this.data = res.Data
							this.total = res.Total
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
						console.log(this.total);
					})
			},
			toForm(item) {
				console.log('选择的客户', item,item.Id)
				if (this.openOrder) {
					let pages = getCurrentPages()
					let prevPage = pages[pages.length-2]
					console.log('获取上一个页面的参数', prevPage.$vm, prevPage.route, prevPage.data)
					// 赋值上一个页面的客户id
					prevPage.$vm.CustomerId=item.Id
					// 调用上一个页面的方法/
					prevPage.$vm.getHistoricalDetail()
					// 赋值上一个页面的客户名称输入框
					prevPage.$vm.setPopupKewords(item.cCusName)
					// 返回到上一页
					uni.navigateBack()
				
				} else {
					if (this.selectType === 'select') {
						// 从货品明细跳转过来  需要获取用户信息
						uni.setStorageSync('customerInfoSelect', item)
						// 返回上一页
						uni.navigateBack({
							delta: 1, //返回层数，2则上上页
						})
						return
					} else if (this.selectType === 'ContactPerson') {
						uni.$emit('updateData', item)
						uni.navigateBack()
						return
					}

					uni.navigateTo({
						url: '/pages/Information/page/CustomerForm?Id=' + item.Id
					})

				}


			},
			cancel() {
				this.queryParam.Keyword = ''
				this.getDataList()
			}
		}
	}
</script>

<style>

</style>